package com.ph.seata.account.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ph.seata.account.dto.AccountDTO;
import com.ph.seata.account.mapper.AccountMapper;
import com.ph.seata.account.model.AccountDO;
import com.ph.seata.account.service.AccountService;
import com.ph.web.common.response.WrapperResponse;
import io.seata.spring.annotation.GlobalLock;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author swallow
 * @since 2021-06-29
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountDO> implements AccountService {

    @Override
    @Transactional
    public WrapperResponse<Object> decreaseAccount(AccountDTO accountDTO) {
        int account = baseMapper.decreaseAccount(accountDTO.getUserId(), accountDTO.getAmount().doubleValue());
        if (account > 0) {
            return WrapperResponse.buildSuccess();
        }
        return WrapperResponse.buildError("失败");
    }

    @Override
    @GlobalLock
    @Transactional(rollbackFor = {Throwable.class})
    public void testGlobalLock() {
        baseMapper.testGlobalLock("1");
        System.out.println("Hi, i got lock, i will do some thing with holding this lock.");
    }

}
